package com.ny.mapper;

import com.ny.entity.Device;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import java.util.List;

@Mapper
public interface DeviceMapper {
    
    /**
     * 查询所有设备信息
     * @return 设备列表
     */
    @Select("SELECT device_id, device_name, device_type, room_id, status, device_sn, protocol, created_time FROM device")
    @Results({
        @Result(property = "deviceId", column = "device_id"),
        @Result(property = "deviceName", column = "device_name"),
        @Result(property = "deviceType", column = "device_type"),
        @Result(property = "roomId", column = "room_id"),
        @Result(property = "status", column = "status"),
        @Result(property = "deviceSn", column = "device_sn"),
        @Result(property = "protocol", column = "protocol"),
        @Result(property = "createdTime", column = "created_time")
    })
    List<Device> findAllDevices();
    
    /**
     * 根据设备名称查询设备信息
     * @param deviceName 设备名称
     * @return 设备列表
     */
    @Select("SELECT device_id, device_name, device_type, room_id, status FROM device WHERE device_name LIKE '%空调%'")
    @Results({
        @Result(property = "deviceId", column = "device_id"),
        @Result(property = "deviceName", column = "device_name"),
        @Result(property = "deviceType", column = "device_type"),
        @Result(property = "roomId", column = "room_id"),
        @Result(property = "status", column = "status")
    })
    List<Device> findAirConditioners();
    
    /**
     * 根据设备名称查询设备信息
     * @param deviceName 设备名称
     * @return 设备列表
     */
    @Select("SELECT device_id, device_name, device_type, room_id, status FROM device WHERE device_name LIKE CONCAT('%',#{deviceName},'%')")
    @Results({
        @Result(property = "deviceId", column = "device_id"),
        @Result(property = "deviceName", column = "device_name"),
        @Result(property = "deviceType", column = "device_type"),
        @Result(property = "roomId", column = "room_id"),
        @Result(property = "status", column = "status")
    })
    List<Device> findDevicesByName(String deviceName);
    
    /**
     * 添加设备
     * @param device 设备对象
     * @return 插入的设备数量
     */
    @Insert("INSERT INTO device(device_sn, device_name, device_type, room_id, status, protocol, created_time) VALUES(#{deviceSn}, #{deviceName}, #{deviceType}, #{roomId}, #{status}, #{protocol}, #{createdTime})")
    int insertDevice(Device device);
}